<?php
/**
 * FBA发货单（国内发fba）
 * (model)
 */

namespace App\Models\Delivery;

use App\Models\Basics\SysPlatAccount;
use App\Models\Basics\SysCountry;
use App\Models\Basics\SysArea;
use Illuminate\Database\Eloquent\Model;
class FBADelivery extends Model
{
    protected $table = 'd_fba_delivery';

    protected $guarded = [];

    public static $_status = [
        1 => '待上传标签',
        2 => '待备货',
        3 => '备货中',
        4 => '待上传箱唛',
        5 => '待发货',
        6 => '已发货',
    ];

    public static $_logisticsStatus = [
        1 => '待更新',
        2 => '已更新',
    ];

    //运单号异常状态
    public static $_trackExStatus = [
        0 => '待录入',
        1 => '未录入',//异常
        2 => '已录入',//正常
    ];

    public static $_exStatus = [
        1 => '待校验',
        2 => '正常',
        3 => '异常',
    ];

    public static $_exDesc = [
        1 => '正常',
        2 => '超7天未发货',//更新后恢复待校验状态
        3 => '发货后超7天未到货',//系统抓取到货数据，有到货记录后恢复待校验状态
        4 => '发货后超20天到货有差异',//销售确认后恢复正常
    ];

    public function detail(){
        return $this->hasMany(FBADeliveryDetail::class,'delivery_no','delivery_no');
    }

    public function labels(){
        return $this->hasMany(FBADeliveryLabels::class,'delivery_no','delivery_no');
    }

    public function marks(){
        return $this->hasMany(FBADeliveryMarks::class,'delivery_no','delivery_no');
    }

    public function logistics(){
        return $this->hasOne(FBADeliveryLogistics::class,'delivery_no','delivery_no');
    }

    public function area()
    {
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public function country()
    {
        return $this->belongsTo(SysCountry::class,'country_id');
    }

    public function account()
    {
        return $this->belongsTo(SysPlatAccount::class,'account_id');
    }

    protected static function boot()
    {
        parent::boot();

        static::deleting(function($delivery) {
            $delivery->detail()->delete();
            if ($delivery->labels()) $delivery->labels()->delete();
            if ($delivery->marks()) $delivery->marks()->delete();
            if ($delivery->logistics()) $delivery->logistics()->delete();
        });
    }

    public static $_model = ['fbaDeliveryDetail' => FBADeliveryDetail::class,'delivery' => self::class];
}
